<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    var socket;
    //判断当前浏览器是否支持websocket编程
    if(window.WebSocket){
        //继续
        socket = new WebSocket("ws://localhost:8081/hello");
        //接受信息
        socket.onmessage = function (ev) {
            var rt = document.getElementById("responseText");
            rt.value = rt.value + "\n" + ev.data;
        }
        //相当于连接开启(感知)
        socket.onopen = function (ev) {
            var rt = document.getElementById("responseText");
            rt.value = "连接开启了";
        }
        //连接关闭
        socket.onclose = function (ev) {
            var rt = document.getElementById("responseText");
            rt.value = rt.value + "\n" + "连接关闭";
        }
        //发送消息到服务器
        function send(message) {
            if(!window.WebSocket){
                return;
            }
            if (socket.readyState == WebSocket.OPEN){
                socket.send(message);
            }else{
                alert("连接没有开启...");
            }
        }
    }else{
        alert("当前浏览器不支持！");
    }
</script>
    <form onsubmit="return false">
        <textarea name="message" style="height: 300px;width :300px"></textarea>
        <input type="button" value="发生消息" onclick="send(this.form.message.value)">
        <textarea id="responseText" style="height: 300px;width: 300px"></textarea>
        <input type="button" value="清空内容" onclick="document.getElementById('responseText').value=''">
    </form>
</body>
</html>